package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.bo.StudentExcel;
import com.tcm.entity.dto.param.StudentPageParam;
import com.tcm.entity.dto.StudentUpdateDto;
import com.tcm.entity.resp.PageR;
import com.tcm.entity.resp.R;
import com.tcm.entity.vo.*;
import com.tcm.exception.DatabaseOperationException;
import com.tcm.service.ClazzService;
import com.tcm.service.StudentService;
import com.tcm.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * PC学生控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/7/2 16:11
 */
@Slf4j
@RestController
@RequestMapping("/pc/student")
public class StudentController {

    @Autowired
    private ClazzService clazzService;// 复用clazzService的代码

    @Autowired
    private StudentService studentService;

    /**
     * 添加单个学生
     * @param studentUpdateDto 学生信息封装对象
     * @param request http请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R addStudent(@RequestBody @Valid StudentUpdateDto studentUpdateDto, HttpServletRequest request){
        boolean res = false;
        try {
            res = studentService.addStudentInfo(studentUpdateDto, AccessTokenUtil.getUserId(request));
        } catch (DatabaseOperationException e) {
            return R.fail(102, e.getMessage());
        }
        return res ? R.success() : R.fail(102, "用户已存在");
    }

    /**
     * 分页获取学生信息
     * @param studentPageParam 分页查询参数封装类
     * @return 分页学生信息
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getStudentListByPage(StudentPageParam studentPageParam){
        PageR<StudentListResult> studentListByPage = studentService.getStudentListByPage(studentPageParam);
        return R.success(studentListByPage);
    }

    /**
     * 修改学生信息
     * @param studentUpdateDto 学生更新信息数据封装对象
     * @param request http请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R modifyStudentInfo(@RequestBody @Valid StudentUpdateDto studentUpdateDto, HttpServletRequest request){
        if (studentUpdateDto.getId() == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean res = false;
        try {
            res = studentService.modifyStudentInfo(studentUpdateDto, AccessTokenUtil.getUserId(request));
        } catch (DatabaseOperationException e) {
            return R.fail(102, e.getMessage());
        }
        return res ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 删除学生信息
     * @param ids 学生ID集合
     * @param request http请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeStudentInfo(@RequestParam("ids") List<String> ids, HttpServletRequest request){
        if (ids == null){
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean res = false;
        try {
            res = studentService.removeStudentInfo(ids, AccessTokenUtil.getUserId(request));
        } catch (Exception e) {
            return R.fail(102, e.getMessage());
        }
        return res ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 学生信息批量上传信息校验
     * @param file excel文件
     * @param request http请求
     * @return 信息校验结果
     */
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public R batchImportStudentInfo(MultipartFile file, HttpServletRequest request){
        BatchImportResult<StudentExcel> result = null;
        try {
            result = studentService.importStudentInfo(file, AccessTokenUtil.getUserId(request));
        } catch (IOException e) {
            return R.fail(0, e.getMessage());
        }
        return R.success(result);
    }

    /**
     * 确认导入学生信息
     * @param uuid 导入uuid
     * @return 响应结果
     */
    @RequestMapping(value = "/import", method = RequestMethod.PUT)
    public R confirmBatchStudentInfo(String uuid, HttpServletRequest request){
        boolean res = studentService.confirmBatchStudentInfo(uuid, AccessTokenUtil.getUserId(request));
        return res ? R.success() : R.fail(ResponseEnum.CACHE_NOT_EXIST);
    }

    /**
     * 导出导入失败学生信息
     * @param uuid 导入uuid
     * @param response http响应对象
     * @return null
     */
    @RequestMapping(value = "/import", method = RequestMethod.GET)
    public R exportImportFailedStudentInfo(String uuid, HttpServletResponse response){
        try {
            studentService.exportImportFailedStudentInfo(uuid, response);
        } catch (Exception e) {
            return R.fail(0, e.getMessage());
        }
        return R.success();
    }

    /**
     * 根据检索信息导出学生信息
     * @param studentPageParam 学生检索信息对象
     * @param response http响应对象
     * @return 响应结果
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public R exportStudentInfo(StudentPageParam studentPageParam, HttpServletResponse response){
        boolean res = studentService.exportStudentInfo(studentPageParam, response);
        return res ? null : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

}
